package hello;

import java.util.List;

import org.hibernate.*;

public class HelloHibernateWorld {

	public static void main(String[] args) {
		
		// save a message 
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		Message message = new Message("Hello Hibernate World.");
		Long msgId = (Long) session.save(message);
		tx.commit();
		session.close();
		
		// retrieve the messages saved
		Session newSession = HibernateUtil.getSessionFactory().openSession();
		Transaction newTransaction = newSession.beginTransaction();
		
		List<Message> messages = (List<Message>)newSession.createQuery("from Message m order by m.text asc").list();
		System.out.println("Message Size: " + messages.size() + "\r\n");
		for(Message m: messages) { 
			System.out.println("\r\n" + m.getText());
		}
		System.out.println("DONE.");
	}

}
